/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or ( at your option ) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "../commonheaders.h"
#include "svcExImport.h"
#include "fileVCF.h"
#include "fileXML.h"
#include "classExImContactBase.h"

#include "m_clui.h"

namespace NExImport {

#define FilterString( hContact ) 	( ( hContact == INVALID_HANDLE_VALUE )?"XMLCard 1.0 ( *.xml )\0*.xml\0DBEditor++ File ( *.ini )\0*.ini\0":"XMLCard 1.0 ( *.xml )\0*.xml\0DBEditor++ File ( *.ini )\0*.ini\0Standard vCard 2.1 ( *.vcf )\0*.vcf\0" )


/***********************************************************************************************************
 * internal variables
 ***********************************************************************************************************/

/**
 * name:	DisplayNameToFileName
 * desc:	convert contact's display name to valid filename
 * param:	hContact	- handle of contact to create the filename for
 *			pszFileName	- buffer, retrieving the converted filename
 *			cchFileName	- number of maximum characters the filename can be
 * return:	nothing
 **/
static VOID DisplayNameToFileName( HANDLE hContact, LPSTR pszFileName, WORD cchFileName )
{
	LPCSTR disp;

	cchFileName--;

	ZeroMemory( pszFileName, cchFileName );

	if( hContact == NULL ) {
		mir_strncpy( pszFileName, Translate( "Owner" ), cchFileName );
	}
	else if( hContact == INVALID_HANDLE_VALUE ) {
		mir_strncpy( pszFileName, Translate( "all Contacts" ), cchFileName );
	}
	else if( disp = ( LPCSTR )CallService( MS_CLIST_GETCONTACTDISPLAYNAME, ( WPARAM )hContact, NULL ) ) {
		// replace unwanted characters
		while( *disp != 0 && cchFileName > 1 ) {
			switch( *disp ) {
				case '?':	case '*':	case ':':
				case '\\':	case '|':	case '/':
				case '<':	case '>':	case '"':
					*( pszFileName++ ) = '_';
					break;
				default:
					*( pszFileName++ ) = *disp;
					break;
			}
			disp++;
			cchFileName--;
		}
	}
}


/***********************************************************************************************************
 * service functions
 ***********************************************************************************************************/

static INT ImportService( WPARAM wParam, LPARAM lParam )
{
	return Import( ( HANDLE )wParam, ( HWND )lParam );
}

static INT ImportAllService( WPARAM wParam, LPARAM lParam )
{
	return Import( INVALID_HANDLE_VALUE, ( HWND )lParam );
}

static INT ExportService( WPARAM wParam, LPARAM lParam )
{
	return Export( ( HANDLE )wParam, ( HWND )lParam );
}

static INT ExportAllService( WPARAM wParam, LPARAM lParam )
{
	return Export( INVALID_HANDLE_VALUE, ( HWND )lParam );
}

/***********************************************************************************************************
 * external module interface
 ***********************************************************************************************************/

/**
 * name:	NExImport::Export
 * desc:	service function to export contact information
 * param:	wParam	- handle to contact or NULL
 *			lParam	- parent window
 * return:	0 always
 **/
INT Export( HANDLE hContact, HWND hwndParent )
{
	CHAR szFileName[MAX_PATH];

	// create the filename to suggest the user for the to export contact
	DisplayNameToFileName( hContact, szFileName, SIZEOF( szFileName ) );
	INT nIndex = MyGetSaveFileName( hwndParent, 
		Translate( "Select a destination file..." ),
		FilterString( hContact ),
		szFileName );

	switch( nIndex ) {
		// .vcf
		case 3:
		{
			CVCardFileVCF vcfFile;

			SetCursor( LoadCursor( NULL, IDC_WAIT ) );
			if( vcfFile.Open( hContact, szFileName, "wt") ) {
				vcfFile.Export( 0 );
				vcfFile.Close();
			}
			SetCursor( LoadCursor( NULL, IDC_ARROW ) );
			return 0;
			/*
			VCardFile vcfFile;
			SetCursor( LoadCursor( NULL, IDC_WAIT ) );
			vcfFile.Export( ( HANDLE )wParam, szFileName );
			SetCursor( LoadCursor( NULL, IDC_ARROW ) );
			break;
			*/
		}
		// .ini
		case 2:
			return NIniFile::Export( hContact, szFileName );

		// .xml
		case 1: {
			CFileXml xmlFile;
			return xmlFile.Export( hContact, szFileName );
		}
	}
	return 1;
}

/**
 * name:	Import
 * desc:	service function to export contact information
 * param:	wParam	- handle to contact or NULL
 *			lParam	- parent window
 * return:	0 always
 **/
INT Import( HANDLE hContact, HWND hwndParent )
{
	CHAR szFileName[MAX_PATH] = { 0 };

	// create the filename to suggest the user for the to export contact
	DisplayNameToFileName( hContact, szFileName, SIZEOF( szFileName ) );

	INT nIndex = MyGetOpenFileName( hwndParent, 
		Translate( "Import User Details from VCard" ),
		FilterString( hContact ),
		szFileName );

	switch( nIndex ) {
		case 1:
		{
			CFileXml xmlFile;
			xmlFile.Import( hContact, szFileName );
	
			return 0;
		}
		// .ini
		case 2:
			return NIniFile::Import( hContact, szFileName );

		// .vcf
		case 3:
		{
			CVCardFileVCF vcfFile;

			if( vcfFile.Open( hContact, szFileName, "rt") ) {
				SetCursor( LoadCursor( NULL, IDC_WAIT ) );
				vcfFile.Import();
				vcfFile.Close();
				SetCursor( LoadCursor( NULL, IDC_ARROW ) );
			}
			return 0;
		}
	}
	return 1;
}

/**
 * name:	vCardLoadModule()
 * desc:	initializes the vCardSupport
 *
 * return:	0 or 1
 **/
VOID LoadModule()
{
	CreateServiceFunction( MS_USERINFO_VCARD_IMPORT, ImportService );
	CreateServiceFunction( MS_USERINFO_VCARD_IMPORTALL, ImportAllService );
	CreateServiceFunction( MS_USERINFO_VCARD_EXPORT, ExportService );
	CreateServiceFunction( MS_USERINFO_VCARD_EXPORTALL, ExportAllService );
}

} // namespace NExImport
